// pages/outvisit/approval/consent.js
var api = require("../../../utils/api.js");
var util = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: 0,//任务ID
    pickerValue: [0],//下拉框的值
    departureAddressList: ["公司", "家里", "其他地方"],//出发地点列表
    startTime: "",//开始时间
    endTime: "",//结束时间
    outvisitTime: "",//外访日期
    outName: "",//外访人员名称
    outIds: [],//外访人员数组
    address: "",//外访地址
    longitude1: 116.46,//表单当前经度
    latitude1: 39.92,//表单当前纬度
    longitude: 116.46,//地图当前经度
    latitude: 39.92,//地图当前纬度
    marker: [//地图标记点
      {
        id: 1,
        longitude: 116.46,
        latitude: 39.92,
        iconPath: "../../../resources/images/icon/map/dibiao.png",
        width: "25px",
        height: "25px"
      }
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var id = options.id;
    // 设置任务ID
    that.setData({
      id: id,
    }, () => {
      // 获取外访审批详情
      that.getOutApprovaDetails();
    });
    // 获取地图组件对象
    that.mapCtx = wx.createMapContext('map', that);
  },

  // 获取外访审批详情
  getOutApprovaDetails: function () {
    var that = this;
    wx.showLoading({
      title: '加载中',
    });//显示加载中
    api.getOutApprovaDetails(that.data.id).then(res => {
      if (res.code == 0) {
        wx.hideLoading();//隐藏加载中
        var data = res.data;
        that.setData({
          outvisitTime: data.setOutTime,
          address: data.address
        }, () => {
          //设置日期选择的区间
          that.setTimeScope();
          //根据位置名称获取坐标
          that.getLocationByName();
        });
      }
    });
  },

  // 设置日期选择的区间
  setTimeScope: function () {
    var that = this;
    var outvisitTime = that.data.outvisitTime;
    var date = outvisitTime.replace(/-/g, '/');
    var timestamp = new Date(date).getTime() + 6 * 24 * 60 * 60 * 1000;

    var startTime = outvisitTime;
    var endTime = util.formatTimeTT(timestamp,"Y-M-D");
    that.setData({
      startTime: startTime,
      endTime: endTime
    });
  },

  //根据位置名称获取坐标
  getLocationByName: function () {
    var that = this;
    //根据位置名称获取坐标
    api.getLocationByName(that.data.address).then(res => {
      var longitude = res.result.location.lng;
      var latitude = res.result.location.lat;
      // 设置标记点
      var marker = that.data.marker;
      marker[0].longitude = longitude;
      marker[0].latitude = latitude;
      that.firstCount = 1;
      that.setData({
        longitude1: longitude,
        latitude1: latitude,
        longitude: longitude,
        latitude: latitude,
        marker: marker
      });
    });
  },

  // 选择外访人员
  selectStaff: function () {
    var that = this;
    wx.navigateTo({
      url: "outvisitStaff"
    });
  },

  // 移动地图选择坐标
  selectLocation: function (e) {
    var that = this;
    // 如果移动完毕
    if (e.type == "end") {
      if (that.firstCount && that.firstCount < 3) {
        that.firstCount++;
        return;
      }
      // 获取当前地图中心的经纬度
      that.mapCtx.getCenterLocation({
        success(res) {
          // 平移标记点，带动画
          that.mapCtx.translateMarker({
            markerId: 1,
            destination: {
              longitude: res.longitude,
              latitude: res.latitude
            },
            duration: 200
          });
          that.setData({
            longitude1: res.longitude,
            latitude1: res.latitude
          }, () => {
            //获取位置名称
            that.getLocationName();
          })
        }
      });
    }
  },

  //获取位置名称
  getLocationName: function () {
    var that = this;
    //根据坐标获取位置名称
    api.getLocationName(that.data.longitude1, that.data.latitude1).then(res => {
      that.setData({
        address: res
      });
    });
  },

  // 提交
  bindFormSubmit: function (e) {
    var that = this;
    var data = e.detail.value;
    if (that.data.outIds.length == 0) {
      wx.showToast({
        title: "请选择外访人员",
        icon: "none"
      });
      return;
    }
    var submitData = {};
    for (var d in data) {
      submitData[d] = data[d];
    }
    submitData.id = that.data.id;
    submitData.outIds = that.data.outIds;
    submitData.assignPersonType = 1;
    api.agreeOut(submitData);
  },

  //时间选择
  bindDateChange(e) {
    let that = this;
    that.setData({
      outvisitTime: e.detail.value
    })
  },

  //下拉框选择事件
  bindPickerChange: function (e) {
    let pickerValue = this.data.pickerValue;
    pickerValue[parseInt(e.currentTarget.dataset.id)] = e.detail.value;
    this.setData({
      pickerValue: pickerValue
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})